import 'dart:convert';

import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
import 'package:flutter/cupertino.dart';
import 'package:geomancy/models/current_city_model.dart';
import 'package:geomancy/models/current_weather_24h_model.dart';
import 'package:geomancy/models/current_weather_7d_model.dart';
import 'package:geomancy/models/current_weather_model.dart';
import 'package:geomancy/models/weather_city_list_model.dart';
import 'package:geomancy/tools/config.dart';
import 'package:geomancy/tools/http.dart';


class WeatherAPI {
  ///APIKEY
  static const String key = '0405a05b714f4bedad1c51f3aca0aec8';

  ///获取用户所在城市
  static Future<AdInfo?> getCurrentCity(BuildContext context) async {
    try {
      final data = await Fetch.request(Method.GET, '/huangli/getCity', context: context);
      final result = CurrentCityModel.fromJson(json.decode(data.data));
      if (result.code == 200) {
        return result.data?.result?.adInfo;
      }
      return null;
    } catch(e) {
      return null;
    }
  }

  ///通过城市名称获取locationID
  static Future<LocationModel?> getCityLocationID(BuildContext context, String name) async {
    try {
      final data = await Fetch.request(Method.GET, '/v2/city/lookup',
          baseUrl: LocalConfig.weatherV2ServerAddress,
          params: {
            "location": name,
          },
          headerOption: {
            "X-QW-Api-Key": key
          },
          context: context
      );
      final result = WeatherCityListModel.fromJson(json.decode(data.data));
      if (result.code == '200' && result.location != null && result.location!.isNotEmpty) {
        return result.location![0];
      }
      return null;
    } catch(e) {
      return null;
    }
  }

  ///获取当前城市实时天气
  static Future<CurrentCityWeatherModel?> getCurrentCityWeather(BuildContext context, String locationId) async {
    final data = await Fetch.request(Method.GET, '/v7/weather/now',
      baseUrl: LocalConfig.weatherServerAddress,
      params: {
        "location": locationId,
      },
      headerOption: {
        "X-QW-Api-Key": key
      },
      context: context
    );
    final result = CurrentWeatherModel.fromJson(json.decode(data.data));
    if (result.code == '200') {
      return result.now;
    }
    return null;
  }

  ///获取当前城市24小时天气预报
  static Future<List<Hourly>?> getCurrentCityWeather24H(BuildContext context, String locationId) async {
    final data = await Fetch.request(Method.GET, '/v7/weather/24h',
      baseUrl: LocalConfig.weatherServerAddress,
      params: {
        "location": locationId,
      },
      headerOption: {
        "X-QW-Api-Key": key
      },
      context: context
    );
    final result = CurrentWeather24HModel.fromJson(json.decode(data.data));
    if (result.code == '200') {
      return result.hourly;
    }
    return null;
  }

  ///获取当前城市7天预报
  static Future<List<Daily>?> get7daysWeather(BuildContext context, String locationId) async {
    final data = await Fetch.request(Method.GET, '/v7/weather/7d',
        baseUrl: LocalConfig.weatherServerAddress,
        params: {
          "location": locationId,
        },
        headerOption: {
          "X-QW-Api-Key": key
        },
        context: context
    );
    final result = CurrentWeather7DaysModel.fromJson(json.decode(data.data));
    if (result.code == '200') {
      return result.daily;
    }
    return null;
  }
}